package com.yvan.leto.server.service;

import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.yvan.YvanUtil;
import com.yvan.leto.server.config.YvanSecurityProperties;
import com.yvan.leto.server.dao.UserAgentMapper;
import com.yvan.leto.server.domain.UserAgent;
import com.yvan.leto.server.security.JwtHelper;
import com.yvan.leto.server.security.PasswordHelper;
import lombok.val;
import org.apache.shiro.authc.ConcurrentAccessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {

    @Autowired
    private UserAgentMapper userAgentMapper;

    @Autowired
    private YvanSecurityProperties yvanSecurityProperties;

    public List<UserAgent> selectByQuery(Pagination pagination) {
        return userAgentMapper.selectByQuery(pagination);
    }


    public Long getTokenVersion(String loginName) {
        val userAgent = userAgentMapper.findByLoginName(loginName);
        if (userAgent == null) {
            return 0L;
        }
        return userAgent.getTokenVersion();
    }

    public UserAgent getAgentByPassword(String loginName) {
        return userAgentMapper.findByLoginName(loginName);
    }

    public String createPassword(UserAgent userAgent, String inputPwd) {
        return PasswordHelper.encryptPassword(inputPwd, yvanSecurityProperties.getSalt());
    }

    public int createUser(UserAgent userAgent) {
        userAgent.setUserAgentId(YvanUtil.createUUID());
        userAgent.setTokenVersion(0L);
        userAgent.setStatus(UserAgent.NORMAL);
        userAgent.setLoginPassword(createPassword(userAgent, userAgent.getLoginPassword()));
        return userAgentMapper.insert(userAgent);
    }

    public UserAgent createUser(String signupName, String signupPwd) {
        if (userAgentMapper.existLoginName(signupName)) {
            throw new ConcurrentAccessException();
        }

        UserAgent userAgent = new UserAgent();
        userAgent.setUserAgentId(YvanUtil.createUUID());
        userAgent.setLoginName(signupName);
        userAgent.setTokenVersion(0L);
        userAgent.setStatus(UserAgent.NORMAL);
        userAgent.setLoginPassword(createPassword(userAgent, signupPwd));
        userAgentMapper.insert(userAgent);

        return userAgent;
    }

    public String createJwt(UserAgent userAgent, boolean rememberMe) {

        return JwtHelper.sign(userAgent.getLoginName(),
                userAgent.getTokenVersion(),
                "user",
                yvanSecurityProperties.getSalt(),
                rememberMe ? yvanSecurityProperties.getTokenExpireRemember() : yvanSecurityProperties.getTokenExpire());
    }

    public UserAgent getByLoginName(String loginName) {
        return userAgentMapper.findByLoginName(loginName);
    }

    public Boolean existLoginName(String loginName) {
        return userAgentMapper.existLoginName(loginName);
    }
}
